package cn.xorange.commons.data.mapper;

import cn.xorange.commons.data.core.entity.BaseEntity;
import cn.xorange.commons.data.core.entity.TreeEntity;
import cn.xorange.commons.data.dao.DaoHelperDao;
import cn.xorange.commons.data.entity.TableColumn;
import org.apache.ibatis.annotations.*;

import java.util.List;
import java.util.Map;

@Mapper
public interface DaoHelperMapper {



    ///**
    // * 获取比对的实体类
    // * @param tableName
    // * @param colName
    // * @param id
    // * @return
    // */
    /// @Select("select * from ${tableName} where ${colName} = #{id}")
    //@SelectProvider(type = DaoHelperDao.class,method = "getEntity")
    /** Map selectEntity(String tableName, String colName, Object id);*/
    @SelectProvider(type = DaoHelperDao.class,method = "getEntity")
    <T extends BaseEntity>Map selectEntity(T t);

    @SelectProvider(type = DaoHelperDao.class,method = "getEntityById")
    <T extends BaseEntity>T selectEntityById(Object id,Class<T> tClass);

    @SelectProvider(type = DaoHelperDao.class,method = "getCount")
    <T extends BaseEntity>int getCount(T t);

    @SelectProvider(type = DaoHelperDao.class,method = "getPTree")
    TreeEntity getTreeById(@Param("tableName") String tableName,
                           @Param("keyCol") String keyCol,
                           @Param("parentId") Object parentId);

    @UpdateProvider(type = DaoHelperDao.class,method = "updateOtherTreeNode")
    void updateOtherTreeNode(@Param("tableName") String tableName,
                             @Param("oldNode") String oldNode,
                             @Param("newNode") String newNode);

    @Select("select count(1) from ${tableName} where find_in_set(#{keyId}, tree_node) and ${keyColName} = #{parentId}")
    int checkChildrenOrg(@Param("tableName") String tableName,
                         @Param("keyColName") String keyColName,
                         @Param("keyId") Object keyId,
                         @Param("parentId") Object parentId);

    @SelectProvider(type = DaoHelperDao.class,method = "getTableColumn")
    List<TableColumn> tableColumns(@Param("dbType") String dbType, @Param("tableName") String tableName);

    @Select("select nextval(#{sequence})")
    Long getKeyByMySQL(@Param("sequence")String sequence);
    @Select("select #{owner}#{sequence}.nextval from dual where rownum=1")
    Long getKeyByOracle(@Param("sequence")String sequence,@Param("owner") String owner);
    @Select("select current_val from sequence WHERE seq_name = #{sequence}")
    Long getKeyBySqlite(@Param("sequence")String sequence);

    @Update("update sequence set current_val = current_val+increment_val where seq_name = #{sequence}")
    Long sqliteNextVal(@Param("sequence")String sequence);


}
